{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2cea949c",
   "metadata": {},
   "source": [
    "# 一.字典的方法\n",
    "\n",
    "\n",
    "* 学习方法  \n",
    "\n",
    "> 1.dict.Tab键 查看所有方法名称\n",
    "\n",
    "> 2.help (dict.function) 查看其中一个方法的具体操作内容"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9df397c6",
   "metadata": {},
   "source": [
    "* 1.clear"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a8e18e8e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method_descriptor:\n",
      "\n",
      "clear(...)\n",
      "    D.clear() -> None.  Remove all items from D.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(dict.clear)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "cad1d43e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Name': 'czx',\n",
       " 'Gender': 'Female',\n",
       " 'Occupation': 'Student',\n",
       " 'Home Planet': 'Shantou'}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person3 = {'Name': 'czx',\n",
    "            'Gender': 'Female',\n",
    "            'Occupation': 'Student',\n",
    "            'Home Planet': 'Shantou'}\n",
    "person3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "6c490bd7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person3.clear()\n",
    "person3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5d9e8c6",
   "metadata": {},
   "source": [
    "* 2.copy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "044b97ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method_descriptor:\n",
      "\n",
      "copy(...)\n",
      "    D.copy() -> a shallow copy of D\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(dict.copy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "2bbeb14f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Name': 'czx',\n",
       " 'Gender': 'Female',\n",
       " 'Occupation': 'Student',\n",
       " 'Home Planet': 'Shantou'}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person3 = {'Name': 'czx',\n",
    "            'Gender': 'Female',\n",
    "            'Occupation': 'Student',\n",
    "            'Home Planet': 'Shantou'}\n",
    "person3.copy()\n",
    "person3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f11c8a09",
   "metadata": {},
   "source": [
    "* 3.fromkeys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ec4f9f8d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function fromkeys:\n",
      "\n",
      "fromkeys(iterable, value=None, /) method of builtins.type instance\n",
      "    Create a new dictionary with keys from iterable and values set to value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(dict.fromkeys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "77954e11",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'N': None, 'a': None, 'm': None, 'e': None}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person3.fromkeys(\"Name\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "885c5259",
   "metadata": {},
   "source": [
    "* 4.get"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "73c75ef4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method_descriptor:\n",
      "\n",
      "get(self, key, default=None, /)\n",
      "    Return the value for key if key is in the dictionary, else default.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(dict.get)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "8c05efeb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Female'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person3.get(\"Gender\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d88a4c5",
   "metadata": {},
   "source": [
    "* 5.items"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "b895f1bf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method_descriptor:\n",
      "\n",
      "items(...)\n",
      "    D.items() -> a set-like object providing a view on D's items\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(dict.items)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "20d0aae9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([('Name', 'czx'), ('Gender', 'Female'), ('Occupation', 'Student'), ('Home Planet', 'Shantou')])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person3.items()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5881c88a",
   "metadata": {},
   "source": [
    "* 6.keys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "c5c87e2e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method_descriptor:\n",
      "\n",
      "keys(...)\n",
      "    D.keys() -> a set-like object providing a view on D's keys\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(dict.keys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "d7939d05",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['Name', 'Gender', 'Occupation', 'Home Planet'])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person3.keys()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa6f7e76",
   "metadata": {},
   "source": [
    "* 7.pop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "7539d7b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method_descriptor:\n",
      "\n",
      "pop(...)\n",
      "    D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n",
      "    \n",
      "    If key is not found, default is returned if given, otherwise KeyError is raised\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(dict.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "2b295b8a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'czx'"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person3.pop(\"Name\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7bf6095b",
   "metadata": {},
   "source": [
    "* 8.popitem"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "94c2efa6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method_descriptor:\n",
      "\n",
      "popitem(self, /)\n",
      "    Remove and return a (key, value) pair as a 2-tuple.\n",
      "    \n",
      "    Pairs are returned in LIFO (last-in, first-out) order.\n",
      "    Raises KeyError if the dict is empty.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(dict.popitem)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "f10fd37f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Home Planet', 'Shantou')"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person3.popitem()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eaa068b4",
   "metadata": {},
   "source": [
    "* 9.setdefault"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "27c7024c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method_descriptor:\n",
      "\n",
      "setdefault(self, key, default=None, /)\n",
      "    Insert key with a value of default if key is not in the dictionary.\n",
      "    \n",
      "    Return the value for key if key is in the dictionary, else default.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(dict.setdefault)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "26b7bc6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Student'"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person3.setdefault(\"Occupation\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce20d9b8",
   "metadata": {},
   "source": [
    "* 10.update"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "d25c00a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method_descriptor:\n",
      "\n",
      "update(...)\n",
      "    D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.\n",
      "    If E is present and has a .keys() method, then does:  for k in E: D[k] = E[k]\n",
      "    If E is present and lacks a .keys() method, then does:  for k, v in E: D[k] = v\n",
      "    In either case, this is followed by: for k in F:  D[k] = F[k]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(dict.update)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "0351938f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gender was found Female time(s).\n",
      "Occupation was found Student time(s).\n"
     ]
    }
   ],
   "source": [
    "for k, v in sorted(person3.items()): \n",
    "    print(k, 'was found', v, 'time(s).') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "609c5347",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gender was found Female time(s).\n",
      "Occupation was found Student time(s).\n"
     ]
    }
   ],
   "source": [
    "for k in sorted(person3): \n",
    "    print(k, 'was found', person3[k], 'time(s).') "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82c2169e",
   "metadata": {},
   "source": [
    "* 11.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "06bd302e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method_descriptor:\n",
      "\n",
      "values(...)\n",
      "    D.values() -> an object providing a view on D's values\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(dict.values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "f1923879",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values(['Female', 'Student'])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person3.values()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12cb39eb",
   "metadata": {},
   "source": [
    "# 二.字典方法的整合"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8e7a4a8",
   "metadata": {},
   "source": [
    "| Syntax    | Description |  example    |  remark     |\n",
    "| :-----------:|:-----------:  | :-----------: | :-----------:  |\n",
    "| clear()  |删除所有的项目|  详见1.clear | 记得运行前面代码  |\n",
    "| copy()  |复制字典中的元素 | 详见2.copy    | 自己运行出来的结果为什么没有double呢...   |\n",
    "| fromkeys()   | (iterable, value=None，/) 键来自iterable，值设置为value | 详见3.fromkeys  | value作为一个可变对象(如空列表)通常没有意义 |\n",
    "| get()    | 如果key在字典中，则返回key的值，否则报错 |  详见4.get      | key不在字典中则报错  |\n",
    "| items()   | 一个类似集合的对象，提供字典的项的视图 | 详见5.items | 返回所有字典内容  |\n",
    "| keys()  |一个类似集合的对象，提供字典的键的视图  |  详见6.keys     | 返回的是所有的键，而不是值  |\n",
    "| pop()    | 删除指定的键并返回相应的值 | 详见7.pop  | 如果key未找到，则返回default，否则引发KeyError  |\n",
    "| popitem()  | 删除并返回一个 (键，值) 对作为二元组 |  详见8.popitem  | 按照后进先出的顺序返回，如果字典为空则引发KeyError异常  |\n",
    "| setdefault()  | 可以把一个不存在的键初始化为一个指定的默认值  | 详见9.setdefault| 使用一个键之前先调用setdefault可以保证避免KeyError |\n",
    "| update()    | 使用键/值对更新字典 | 详见10.update  |接受另一个字典对象或键/值对的可迭代对象|\n",
    "| values()    | 返回字典值的新视图 | 详见11.values  |一个dict.values()视图与另一个视图之间的相等比较总是返回False |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c1370e59",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
